package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@RestController
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> findBrandById(@PathVariable("id")Long id){
        return ResponseEntity.ok(new BrandDTO(brandService.getById(id)));
    }

    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> findBrandByIds(@RequestParam("ids")List<Long> ids){
        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.listByIds(ids)));
    }

    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> findBrandByCategoryId(@RequestParam("id")Long cid){
        return ResponseEntity.ok(BrandDTO.convertEntityList(brandService.findBrandByCategoryId(cid)));
    }

    @GetMapping("page")
    public ResponseEntity<PageDTO> findBrandBypage(@RequestParam(value = "page",required = true,defaultValue = "1")Integer page,
                                                   @RequestParam(value = "rows",required = true,defaultValue = "5")Integer rows,
                                                   @RequestParam(value = "key",required = false)String key){
        return ResponseEntity.ok(brandService.findBrandBypage(page,rows,key));
    }

    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO){
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Void> deleteBrandById(@PathVariable("id")Long id){
        brandService.deleteBrandById(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 将图片名称存入redis中用于定时清理无用图片
     * @return
     */
    @GetMapping("saveImgName")
    public ResponseEntity<Void> saveImgName(@RequestParam("imgName") String imgName){
        brandService.saveImgName(imgName);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
}
